package com.example.podcaster.util;

import org.holoeverywhere.ArrayAdapter;
import org.holoeverywhere.app.AlertDialog;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.podcaster.R;

public class EpisodeListAdapter extends ArrayAdapter<String>
{
    private Context context;

    private final String[] names;

    private int[] ids;
    
    private final boolean[] downloaded;

    SharedPreferences preferences;
    
    private View rowView;

    public EpisodeListAdapter(Context context, String[] names, int[] ids, boolean[] downloaded)
    {
        super(context, R.layout.episode_entry, names);
        this.context = context;
        this.names = names;
        this.downloaded = downloaded;
        this.ids = ids;

        preferences = context.getSharedPreferences("prefs", 0);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        final int i = position;
        rowView = inflater.inflate(R.layout.episode_entry, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.textView);
        setUpRankingButtons(position, names, rowView);
        final ImageView diskView = (ImageView) rowView.findViewById(R.id.diskButton);

        textView.setText(names[i]);

        if (downloaded[i])
        {
            diskView.setImageResource(R.drawable.disk_filled);

        }
        else
        {
            diskView.setImageResource(R.drawable.disk_hollow);
        }

        return rowView;
    }

    public void setUpRankingButtons(int i, String[] n, View rowView)
    {
        final ImageView[] ratingButtons = new ImageView[5];
        ImageView button1 = (ImageView) rowView.findViewById(R.id.listReviewButton1);
        ratingButtons[0] = button1;
        final int index = i;
        final String[] names = n;
        button1.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                int i = 0;
                Activity act = (Activity) context;
                SharedPreferences preferences = act.getSharedPreferences("prefs", 0);
                SharedPreferences.Editor editor = preferences.edit();
                if (preferences.contains(ids[index] + "_" + names[index] + "_rating"))
                {
                    i = preferences.getInt(ids[index] + "_" + names[index] + "_rating", 0);

                    editor.remove(ids[index] + "_" + names[index] + "_rating");
                    if (i == 1)
                    {
                        editor.putInt(ids[index] + "_" + names[index] + "_rating", 0);
                    }
                    else if (i != 0)
                    {
                        editor.putInt(ids[index] + "_" + names[index] + "_rating", 1);

                    }
                    else
                    {
                        editor.putInt(ids[index] + "_" + names[index] + "_rating", 1);
                    }
                }
                else
                {
                    editor.putInt(ids[index] + "_" + names[index] + "_rating", 1);
                }

                editor.commit();
                resetRatingIcons(index, ratingButtons);
            }
        });
        ImageView button2 = (ImageView) rowView.findViewById(R.id.listReviewButton2);
        button2.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                int i = 0;
                SharedPreferences.Editor editor = preferences.edit();
                if (preferences.contains(ids[index] + "_" + names[index] + "_rating"))
                {
                    i = preferences.getInt(ids[index] + "_" + names[index] + "_rating", 0);

                    editor.remove(ids[index] + "_" + names[index] + "_rating");
                    if (i == 0)
                    {
                        i = 1;
                    }
                    if (ratingButtons[i - 1] == v)
                    {
                        editor.putInt(ids[index] + "_" + names[index] + "_rating", 0);
                    }
                    else
                    {
                        editor.putInt(ids[index] + "_" + names[index] + "_rating", 2);
                    }
                }
                else
                {
                    editor.putInt(ids[index] + "_" + names[index] + "_rating", 2);
                }
                editor.commit();
                resetRatingIcons(index, ratingButtons);
            }
        });
        ratingButtons[1] = button2;
        ImageView button3 = (ImageView) rowView.findViewById(R.id.listReviewButton3);
        button3.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                int i = 0;
                Activity act = (Activity) context;
                preferences = act.getSharedPreferences("prefs", 0);
                SharedPreferences.Editor editor = preferences.edit();
                if (preferences.contains(ids[index] + "_" + names[index] + "_rating"))
                {
                    i = preferences.getInt(ids[index] + "_" + names[index] + "_rating", 0);

                    editor.remove(ids[index] + "_" + names[index] + "_rating");
                    if (i == 0)
                    {
                        i = 1;
                    }
                    if (ratingButtons[i - 1] == v)
                    {
                        editor.putInt(ids[index] + "_" + names[index] + "_rating", 0);
                    }
                    else
                    {
                        editor.putInt(ids[index] + "_" + names[index] + "_rating", 3);
                    }
                }
                else
                {
                    editor.putInt(ids[index] + "_" + names[index] + "_rating", 3);
                }
                editor.commit();
                resetRatingIcons(index, ratingButtons);
            }
        });
        ratingButtons[2] = button3;
        ImageView button4 = (ImageView) rowView.findViewById(R.id.listReviewButton4);
        button4.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                int i = 0;
                Activity act = (Activity) context;
                preferences = act.getSharedPreferences("prefs", 0);
                SharedPreferences.Editor editor = preferences.edit();
                if (preferences.contains(ids[index] + "_" + names[index] + "_rating"))
                {
                    i = preferences.getInt(ids[index] + "_" + names[index] + "_rating", 0);

                    editor.remove(ids[index] + "_" + names[index] + "_rating");
                    if (i == 0)
                    {
                        i = 1;
                    }
                    if (ratingButtons[i - 1] == v)
                    {
                        editor.putInt(ids[index] + "_" + names[index] + "_rating", 0);
                    }
                    else
                    {
                        editor.putInt(ids[index] + "_" + names[index] + "_rating", 4);
                    }
                }
                else
                {
                    editor.putInt(ids[index] + "_" + names[index] + "_rating", 4);
                }
                editor.commit();
                resetRatingIcons(index, ratingButtons);
            }
        });
        ratingButtons[3] = button4;
        ImageView button5 = (ImageView) rowView.findViewById(R.id.listReviewButton5);
        button5.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                int i = 0;
                Activity act = (Activity) context;
                preferences = act.getSharedPreferences("prefs", 0);
                SharedPreferences.Editor editor = preferences.edit();
                if (preferences.contains(ids[index] + "_" + names[index] + "_rating"))
                {
                    i = preferences.getInt(ids[index] + "_" + names[index] + "_rating", 0);

                    editor.remove(ids[index] + "_" + names[index] + "_rating");
                    if (i == 0)
                    {
                        i = 1;
                    }
                    if (ratingButtons[i - 1] == v)
                    {
                        editor.putInt(ids[index] + "_" + names[index] + "_rating", 0);
                    }
                    else
                    {
                        editor.putInt(ids[index] + "_" + names[index] + "_rating", 5);
                    }
                }
                else
                {
                    editor.putInt(ids[index] + "_" + names[index] + "_rating", 5);
                }
                editor.commit();
                resetRatingIcons(index, ratingButtons);
            }
        });
        ratingButtons[4] = button5;
        resetRatingIcons(index, ratingButtons);
    }

    public void setPreferences(SharedPreferences prefs)
    {
        preferences = prefs;
        for (int i = 0; i < names.length; i++)
        {
            setUpRankingButtons(i, names, rowView);
        }

    }

    public void resetRatingIcons(int index, ImageView[] ratingButtons)
    {
        int i = 0;
        if (preferences.contains(ids[index] + "_" + names[index] + "_rating"))
        {
            i = preferences.getInt(ids[index] + "_" + names[index] + "_rating", 0);
        }
        for (int j = 0; j < ratingButtons.length; j++)
        {
            if (j < i)
            {
                ratingButtons[j].setBackgroundResource(R.drawable.heart_filled);
            }
            else if (i == 0)
            {
                ratingButtons[j].setBackgroundResource(R.drawable.heart_hollow);
            }
            else
            {
                ratingButtons[j].setBackgroundResource(R.drawable.heart_hollow);
            }
        }
    }
}
